<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Utils\GDUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    /**
     * @route   root/login-act
     * @auth    安全管理
     * @desc    管理员登录处理
     */
    public function loginAct(Request $request)
    {
        if (!$this->validateReq($request, Admin::loginRulesWithMsg(0), Admin::loginRulesWithMsg(1))) {
            $this->refreshYZM();
            $this->data = $this->makeJsCode(1, $this->errors->first());
            return $this->apiJson();
        }
        //效验验证码
        if (ss(GDUtil::YZM_ADMIN_CODE) != $request->yzm) {
            $this->refreshYZM();
            $this->data = $this->makeJsCode(1, "验证码错误！");
            return $this->apiJson();
        }

        //效验账号密码
        if ($admin = Admin::loginValid($request)) {
            $admin->loginStatus()->refreshIP($request);
            $this->data = $this->makeJsCode(0, "登录成功！");
            $this->data['id'] = $admin->id;
            $this->data['name'] = $admin->username;
        } else {
            $this->data = $this->makeJsCode(1, "账号或密码错误！");
        }
        return $this->apiJson();
    }
}
